package com.xyhuo.leetcode.november;

import java.util.*;

/**
 * 无重复字符的最长子串
 * https://leetcode.cn/problems/longest-substring-without-repeating-characters/
 *
 * @time 2023/11/28
 */
public class LengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String s) {
        char[] charArray = s.toCharArray();
        Queue<Character> queue = new LinkedList<>();
        int len = charArray.length;
        int ans = 0;
        for (int i = 0; i < len; i++) {
            if (queue.isEmpty()) {
                queue.add(charArray[i]);
                if (ans < 1) {
                    ans = 1;
                }
            } else {
                if (queue.contains(charArray[i])) {
                    while (!Objects.equals(queue.peek(), charArray[i])) {
                        queue.remove();
                    }
                }
                if (Objects.equals(queue.peek(), charArray[i])) {
                    queue.remove();
                }
                queue.add(charArray[i]);
                if (queue.size() > ans) {
                    ans = queue.size();
                }
            }
        }
        return ans;
    }

//    public static void main(String[] args) {
//
//        LengthOfLongestSubstring ll = new LengthOfLongestSubstring();
//        int i = ll.lengthOfLongestSubstring("pwwkew");
//        System.out.println(i);
//    }
}
